package com.zpark.express.service.impl;

import com.zpark.express.constant.Constants;
import com.zpark.express.domain.Admin;

import com.zpark.express.mapper.AdminMapper;
import com.zpark.express.result.Result;
import com.zpark.express.service.AdminService;
import com.zpark.express.util.VerifyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    private AdminMapper adminMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
    * @Desc: 管理员登录
    * @Date: 2020/7/17
    */
    @Override
    public Admin findByNameAndPwd(Admin admin) {
        try {
            return  adminMapper.findByNameAndPwd(admin);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }


    /**
    * @Desc: 查询所有管理员
    * @param:
    * @return:
    * @Author: GOGOGO
    * @Date: 2020/7/18
    */
    @Override
    public Result findAll() {
        try {
            List<Admin> adminList=adminMapper.findAll();
            if(adminList!=null && adminList.size()>0){
                return Result.addResultOk(adminList,Constants.SELECT_SUCCESS);
            }
            return Result.fail(Constants.SELECT_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    /**
    * @Desc: 根据id修改管理员信息
    * @Date: 2020/7/18
    */
    @Override
    public Result updateByAdminId(Admin admin) {
        try {

            if(admin.getAdminId()==null || "".equals(admin.getAdminId())){
                return Result.fail(Constants.ID_NULL);
            }
            //校验邮箱
            if(!StringUtils.isEmpty(admin.getAdminEmail())){
                Boolean flag = VerifyUtil.verifyEmail(admin.getAdminEmail());
                if(!flag){
                    //返回false,验证不通过
                    return Result.fail(Constants.EMAIL_ERROE);
                }
            }

            //校验手机格式
            if(!StringUtils.isEmpty(admin.getAdminPhone())){
                Boolean flag = VerifyUtil.verifyPhone(admin.getAdminPhone());
                if(!flag){
                    //返回false,验证不通过
                    return Result.fail(Constants.PHONE_ERROE);
                }
            }

            int i=adminMapper.updateByAdminId(admin);
            if(i>0){
                 return Result.success(Constants.UPDATE_SUCCESS);
            }
            return Result.fail(Constants.UPDATE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    /**
    * @Desc: 根据id删除管理员
    * @Date: 2020/7/18
    */
    @Override
    public Result deleteById(int adminId) {
        try {

          //  if(adminId<0 || "null".equals(String.valueOf(adminId))  || " ".equals(adminId+"")) {

              if(adminId<0 || StringUtils.isEmpty(adminId+"")){
                return Result.fail(Constants.ID_NULL);
            }
            int i=adminMapper.deleteById(adminId);
            if(i>0){
                return Result.success(Constants.DELETE_SUCCESS);
            }
            return Result.fail(Constants.DELETE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
}
